package com.app114.app.android.rk.webkit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage.QuotaUpdater;
import android.webkit.WebView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.app114.app.android.rk.R;
import com.app114.app.android.rk.log.Logger;

public class ExtendedWebChromeClient extends WebChromeClient {
	private static final String TAG = ExtendedWebChromeClient.class.getSimpleName();
	private Activity mActivity;
	private ActionBar mActionBar;
	private WebChromeClientListener mListener;
	public String mTitle;
	public Bitmap mIcon;

	public ExtendedWebChromeClient(WebChromeClientListener mListener) {
		this.mActivity = (Activity) mListener;
		this.mListener = mListener;
	}

	@Override
	public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
		// TODO Auto-generated method stub
		return super.onJsAlert(view, url, message, result);
	}

	@Override
	public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
		// TODO Auto-generated method stub
		return super.onJsBeforeUnload(view, url, message, result);
	}

	@Override
	public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
		// TODO Auto-generated method stub
		return super.onJsConfirm(view, url, message, result);
	}

	@Override
	public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
		// TODO Auto-generated method stub
		return super.onJsPrompt(view, url, message, defaultValue, result);
	}

	@Override
	public boolean onJsTimeout() {
		// TODO Auto-generated method stub
		return super.onJsTimeout();
	}

	@Override
	public void onProgressChanged(WebView view, int newProgress) {
		if (mActivity != null && mActivity instanceof SherlockFragmentActivity) {
			mActionBar = ((SherlockFragmentActivity) mActivity).getSupportActionBar();
			mActionBar.setTitle("加载中..." + newProgress + "%");
			if (newProgress == 100) {
				mActionBar.setTitle(mTitle != null && mTitle.equals("") ? mActivity.getResources().getString(R.string.app_name) : mTitle);
			}
		}
		super.onProgressChanged(view, newProgress);
	}

	@Override
	public void onGeolocationPermissionsShowPrompt(final String origin, final Callback callback) {
		callback.invoke(origin, true, false);
		final boolean remember = false;
		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle("Locations");
		builder.setMessage("Would like to use your Current Location ").setCancelable(true)
				.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// origin, allow, remember

						callback.invoke(origin, true, remember);
					}
				}).setNegativeButton("Don't Allow", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// origin, allow, remember
						callback.invoke(origin, false, remember);
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
		Logger.i(TAG, consoleMessage.message());
		return super.onConsoleMessage(consoleMessage);
	}

	@Override
	public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota, QuotaUpdater quotaUpdater) {
		quotaUpdater.updateQuota(spaceNeeded * 2);
	}

	@Override
	public void onReceivedIcon(WebView view, Bitmap icon) {
		mIcon = icon;
		super.onReceivedIcon(view, icon);
	}

	@Override
	public void onReceivedTitle(WebView view, String title) {
		mTitle = title;
		super.onReceivedTitle(view, title);
	}

	public interface WebChromeClientListener {

	}

}
